#!/usr/bin/env python
# encoding: utf-8
"""
NetworkNode.py

Created by Bud Gibson on 2010-07-20.
Copyright (c) 2010 Bud Gibson. Available under the GPL/Artistic License.
"""
from ZODB import DB
from ZODB.FileStorage import FileStorage
from ZODB.PersistentMapping import PersistentMapping
from persistent import Persistent
import transaction

class NetworkNode(Persistent):
  """Represents a network participant"""
  def __init__(self, dico):
    super(NetworkNode, self).__init__()
    self.id = dico['id']
    self.displayName = dico['displayName']
    self.profileUrl = dico['profileUrl']
    self.thumbnailUrl = dico['thumbnailUrl']
    self.depth = dico['depth']
    self._ties = []
    self._net_ties = []
    self.public = 'unknown'
    
  def add_tie(self, node):
    """Adds a persistent node"""
    self._ties.append(node)
    self._p_changed = True
    
  def add_net_tie(self, node):
    """
    Adds a link back to the node linking here. Useful for people who 
    do not share following and followers.
    """
    self._net_ties.append(node)
    self._p_changed = True
    
  @property
  def count_ties(self):
    """The number of ties"""
    return len(self._ties)
    
  @property
  def count_net_ties(self):
    """The number of in-network ties"""
    return len(self._net_ties)
    
  @property
  def ties(self):
    """gets the _ties sorted by displayName"""
    return self._ties.sort(sort_displayName)
    
  @property
  def net_ties(self):
    """gets the _net_ties sorted by displayName"""
    return self._net_ties.sort(sort_displayName)
  
  @property
  def reach(self):
    """docstring for reach"""
    if self.public == True:
      return sum([v.count_ties for v in self._ties])
    if self.public == False:
      return sum([v.count_ties for v in self._net_ties])
    if self.public == 'unknown':
      return 'unknown'

if __name__ == '__main__':
  pass